//
//  HINavigationBar.swift
//  Health_Infomation
//  Created by 林帆 on 2017/8/17.
//  Copyright © 2017年 林帆. All rights reserved.

import UIKit

@objc protocol HINavigationBarDelegate {
      func HINavigationPushVC()
      func HINavigationBackVC()
}

class HINavigationBar: UINavigationBar {
   
    weak var NavDelegate : HINavigationBarDelegate?
    fileprivate lazy var _textLabel  = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 30))
    fileprivate lazy var _navItem = UINavigationItem()
    //右侧收藏按钮
     var collectionBtnTitle :String?  {
         didSet {
            let collectionBtn = UIButton(frame: CGRect(x: 0, y: 0, width: 80, height: 20))
            collectionBtn.setTitle(collectionBtnTitle, for: .normal)
            collectionBtn.titleLabel?.textAlignment = NSTextAlignment.right
            collectionBtn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
            collectionBtn.setTitleColor(kRGBColorFromHex(rgbValue: 0xffffff), for: .normal)
            collectionBtn.addTarget(self, action: #selector(pushToCollectionV), for: .touchUpInside)

            //将collectionBtn添加到_navItem
            let rightButton = UIBarButtonItem(customView: collectionBtn)
            _navItem.setRightBarButton(rightButton, animated: true)
        }
    }
//    fileprivate var collectionBtnTitle :String? {
//        get {
//            return _collectionBtnTitle
//        }
//        set (value) {
//            let collectionBtn = UIButton(frame: CGRect(x: 0, y: 0, width: 80, height: 20))
//            collectionBtn.setTitle(value, for: .normal)
//            collectionBtn.titleLabel?.textAlignment = NSTextAlignment.right
//            collectionBtn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
//            collectionBtn.setTitleColor(kRGBColorFromHex(rgbValue: 0xffffff), for: .normal)
//            collectionBtn.addTarget(self, action: #selector(pushToCollectionV), for: .touchUpInside)
//            
//            //讲collectionBtn添加到_navItem
//            let rightButton = UIBarButtonItem(customView: collectionBtn)
//            _navItem.setRightBarButton(rightButton, animated: true)
//        }
//    }
    
    func ConfigUI(title : String){
        //设置 TitleView
        _textLabel.text = title
        _textLabel.textAlignment = NSTextAlignment.center
        _textLabel.font = UIFont.systemFont(ofSize: 18)
        _textLabel.textColor = UIColor.white
        _navItem.titleView = _textLabel
        
        //设置 LeftBarButton
        let leftBtn = UIButton(frame: CGRect(x: 0, y: 0, width: 40, height: 30))
        leftBtn.setImage(UIImage(named: "返回"), for: .normal)
        leftBtn.addTarget(self, action: #selector(backtoLastVC), for: .touchUpInside)
        _navItem.setLeftBarButton(UIBarButtonItem(customView: leftBtn), animated: true)
        self.pushItem(_navItem, animated: true)
    }
    
    func pushToCollectionV() {
        print("pushToCollectionV is touched off")
        NavDelegate?.HINavigationPushVC()
    }
    
    func backtoLastVC() {
        NavDelegate?.HINavigationBackVC()
    }
    
    // 1.
    convenience init(title: String) {
        self.init()
        ConfigUI(title: title)
    }
    
//    2.
//    convenience init(title: String, collectionTitle: String) {
//        self.init()
//        ConfigUI(title: title)
//    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = kRGBColorFromHex(rgbValue: 0x333333)
        
        // 透明
        self.shadowImage = UIImage()
        self.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
